Ext.onReady(function(){
  Ext.QuickTips.init();

  // maintain window state automatically
  var win = new Ext.air.NativeWindow({
    id        : 'mainWindow',
    instance  : window.nativeWindow,
    width     : 800,
    height    : 600,

    // System tray config
    minimizeToTray  : true,
    trayIcon        : 'icons/AIRApp_16.png',
    trayTip         : 'airPodder',
    trayMenu        : [{
      text    : 'Open airPodder',
      handler : function(){
        win.activate();
      }
    }, '-', {
      text    : 'Exit',
      handler : function(){
        air.NativeApplication.nativeApplication.exit();
      }
    }]
  });

  Airpodder.data.conn.open('airpodder.db');
// Airpodder.data.channels.prepareTable();
//  Airpodder.data.episodes.prepareTable();
//  Airpodder.data.channels.loadDemoChannels();
  //Airpodder.data.episodes.loadDemoEpisodes();

//  Airpodder.util.FeedParser.getFeed('http://radio-t.com/category/podcasts/feed/');


  var menus = Ext.air.SystemMenu;
  menus.add('Podcasts', [{
    text    :'Exit',
    handler : function(){air.NativeApplication.nativeApplication.exit();}
  }]);
  menus.add('Subsriptions', [{
    text    :'Subscribe to new podcast URL',
    handler : function(){Ext.air.NativeWindowManager.getAddChannelWindow().activate();}
  }]);
  menus.add('Episodes', [{
    text    :'Exit',
    handler : function(){air.NativeApplication.nativeApplication.exit();}
  }]);
  menus.add('MP3 player', [{
    text    :'Exit',
    handler : function(){air.NativeApplication.nativeApplication.exit();}
  }]);
  menus.add('View', [{
    text    :'Exit',
    handler : function(){air.NativeApplication.nativeApplication.exit();}
  }]);
  menus.add('Help', [{
    text    : 'About',
    handler : function(){air.NativeApplication.nativeApplication.exit();}
  }]);

  var tb = new Ext.Toolbar({
    region  :'north',
    id      :'main-tb',
    height  :26,
    items   : [{
      xtype     : 'tbbutton',
      cls       : 'x-btn-text-icon',
//      disabled  : true,
      iconCls   : 'x-download-icon',
      text      : 'Download',
      handler   : function () {}
    }, {
      xtype     : 'tbbutton',
      cls       : 'x-btn-text-icon',
      disabled  : true,
      icon      : 'icons/play_16.png',
      text      : 'Play',
      handler   : function () {}
    }, {
      xtype     : 'tbbutton',
      cls       : 'x-btn-text-icon',
      disabled  : true,
      icon      : 'icons/player_16.png',
      text      : 'Transfer',
      handler   : function () {}
    }, {
      xtype     : 'tbbutton',
      cls       : 'x-btn-text-icon',
      disabled  : true,
      icon      : 'icons/cancel_16.png',
      text      : 'Cancel',
      handler   : function () {}
    }, '-', {
      xtype     : 'tbbutton',
      cls       : 'x-btn-icon',
      disabled  : true,
      icon      : 'icons/preferences_16.png',
      handler   : function () {}
    }, '-', {
      xtype   : 'tbbutton',
      cls     : 'x-btn-icon',
      icon    : 'icons/exit_16.png',
      handler : function () {air.NativeApplication.nativeApplication.exit();}
    }]
  });
  
  var channel_grid = new Airpodder.ui.ChannelGrid();
  var episode_grid = new Airpodder.ui.EpisodeGrid();
  var channel_selection = channel_grid.getSelectionModel();
  
  channel_selection.on('rowselect', function(sm,index,record) {
    episode_grid.showForChannel(record.data.id); 
    //return this.fireEvent('channelselected', record.data.id);
  });

  var viewport = new Ext.Viewport({
    layout  : 'border',
    items   : [tb, {
      xtype     : 'tabpanel',
      region    : 'center',
      activeTab : 0,
      frame     : true,
      items     : [{
        title : 'Podcasts (7)',
        layout: 'border',
        items : [{
          region  : 'west',
          margins : '5 0 5 5',
          width   : 200,
          split   : true,
          layout  : 'fit',
          minSize : 100,
          maxSize : 300,
          items   : [channel_grid]
        }, {
          region  :'center',
          layout  : 'fit',
          margins : '5 5 5 0',
          items   : [episode_grid]
        }]
      }, {
        title     : 'Downloads',
        layout    : 'fit',
        id        : 'downloads_tab',
        items     : [{xtype: 'downloadergrid', id: 'downloader_grid'}]
      }]
    }]
  });

  win.show();
  win.instance.activate();

  win.on('closing', function(){
    Ext.air.NativeWindowManager.closeAll();
  });
});